//
//  HomeTableViewCell.m
//  HarmonizePub
//
//  Created by rzhy on 2016/11/1.
//  Copyright © 2016年 Zy. All rights reserved.
//

#import "HomeTableViewCell.h"
#import "Config.h"
#import "TableViewCollectionCell.h"

static NSString * CellIdentifier = @"Cell";
CGFloat const collectionCellHeight = 81.0f;
CGFloat const plusHeight = 105.0f;

@interface HomeTableViewCell () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

@implementation HomeTableViewCell

+ (NSString *)registerName {
    return @"HomeTableViewCell";
}

+ (CGFloat)cellHeightWithDataArray:(NSArray *)dataArray {
    CGFloat height = collectionCellHeight;
    if (iPhone6P) height = plusHeight;
    NSInteger count = roundf(dataArray.count / 2.0);
    CGFloat cellHeight = count * height;
    if (cellHeight < height) return height;
    return cellHeight;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    CGFloat itemSize = XHScreenWidth / 2;
    flowLayout.itemSize = CGSizeMake(itemSize, collectionCellHeight);
    if (iPhone6P) flowLayout.itemSize = CGSizeMake(itemSize, plusHeight);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    self.collectionView.collectionViewLayout = flowLayout;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    UINib *nib = [UINib nibWithNibName:[TableViewCollectionCell registerName] bundle:nil];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:CellIdentifier];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {}

- (void)setDataArray:(NSArray *)dataArray {
    _dataArray = dataArray;
    [self.collectionView reloadData];
}

#pragma mark - collectionView delegate/dataSource Method
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    [cell setCellShowDataDictionary:self.dataArray[indexPath.item]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
//    XHLog(@"dataDict: %@", self.dataArray[indexPath.item])
    if ([self.delegate respondsToSelector:@selector(tableViewCell:didSelectedItemAtDataDictionary:)])
        [self.delegate tableViewCell:self didSelectedItemAtDataDictionary:self.dataArray[indexPath.item]];
}

@end
